/**
 * Auth controller js code
 */
var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
define(function (require, exports, module) {
    //var template = require("template");
    require("JTemplate");

    require("ajaxproxy");
    require("layer");

    var common = require('common');
    
    
    exports.init = function (rowIndex,product_name,factory_name,brand_name,tpfb,type_name) {

        //添加数据
        $('[data-act=do_add]').on("click", function () {
            
            if(!doSumbit()){
                return false;
            }

            var data = $("#addOfferFrom").serialize();
            var action = $("#addOfferFrom").attr('action');
            $.post(action, data, function (result) {
                var ajax = $.parseJSON(result);
                if (ajax.code == "000") {
                    layer.msg('提交成功',{shift: -1}, function() {
                        //window.location.href='/mobadmin/offer/index';
                        window.location.href='/mobadmin/offer/detail?offer_id='+ajax.data.offer_id+"&shareSuccessPanel=1";

                    });
                } else {
                    layer.msg("提交失败");
                }
            });
        });
        if(rowIndex){
            if(window.localStorage){
                var tableHtml = localStorage.getItem("modifyTableHtml");
                var tableInpStr = localStorage.getItem("modifyTableInpStr");
                tableInpVals = tableInpStr.split(",");
                
                if(tableHtml && tableHtml != ""){
                    $(".detailTable").addClass("modifyMode").html(tableHtml);
                    //$(".delBtn").css("display","block");
                    $(".tabNumberInp").each(function(i){
                       $(this).val(tableInpVals[i]); 
                    });
                    
                   
                    var Col = $(".detailTable tr[data-id=" +rowIndex + "] .pinmingCol");
                    Col.addClass("pinmingColSel");
                    Col.attr("data-tpfb",tpfb);
                    Col.find(".txt_pro").html(product_name);
                    Col.find(".txt_fac").html(factory_name);
                    Col.find(".txt_bra").html(brand_name);
                    Col.attr("data-type",type_name);
                    //data_id = $(".bjtable tr:last").attr("data-id");
                    modifyFun();
                }
                
            }
        }
    };

    function doSumbit(){

        var offerProducts=[];//报价产品信息
        var bool=true;

        $(".bjtable tr:gt(0)").each(function(){


            var select_tr_obj=$(this);

            var select_input_obj=select_tr_obj.find('.pinmingCol');

            var type_name=select_input_obj.data('type');
            var product_name=select_input_obj.find('.txt_pro').html();
            var factory_name=select_input_obj.find('.txt_fac').html();
            var brand_name=select_input_obj.find('.txt_bra').html();
            var tpfb=select_input_obj.data('tpfb');
            var quantity   = select_tr_obj.find('[data-act=quantity]').val();
            var sales_price= select_tr_obj.find('[data-act=sales_price]').val();

            var product='{"type_name":"'+type_name+'","product_name":"'+product_name+'","factory_name":"'+factory_name+'","brand_name":"'+brand_name+'","tpfb":"'+tpfb+'","quantity":"'+quantity+'","sales_price":"'+sales_price+'"}';

            offerProducts.push(product);

            console.log(offerProducts);
            if(tpfb==''||type_name==''||product_name==''||factory_name==''||brand_name==''||quantity==''||sales_price==''){
                bool=false;
                return false;
            }
        });

        var ss='['+offerProducts.join(',')+']';

        $('input[name="data[offer_products]"]').val(ss);

        if($('input[name="data[contact_mobile]"]').val()==''||$('input[name="data[contact]"]').val()==''){
            layer.msg('请输入联系方式和公司名');

            return false;
        }
        var mobile=$('input[name="data[contact_mobile]"]').val();
        var mobile2=$('input[name="data[contact_mobile2]"]').val();

        if(!(checkPhone(mobile)||checkTel(mobile))){
            layer.msg('联系方式格式不正确');
            return false;
        }

        if(mobile2!=''&&!(checkPhone(mobile2)||checkTel(mobile2))){
            layer.msg('联系方式格式不正确');
            return false;
        }

        if(!bool){
            layer.msg('请输入完整信息');
        }


        return bool;

    }

});

function modifyFun(){
    $(".modifyInp").show();
    $(".modifyTxt").hide();
    $(".modifyBtn").hide();
    $(".delBtn").css("display","block");
    $(".saveBtn").show();
    $(".detailTable").addClass("modifyMode");
    $(".baojiaRow").addClass("baojiaRowEdit");
    
    if($("#phoneInp2").val() != ""){
        $("#addPhoneBtn").hide();
        $("#delPhoneBtn").show();
    }else{
        $("#addPhoneBtn").show();
        $("#delPhoneBtn").hide();
    }
    
    $(".detailTable .col1").click(function(){
        var radioBtn = $(this).find(".radioBtn");
        if(radioBtn.hasClass("radioSel")){
            radioBtn.removeClass("radioSel");
        }else{
            radioBtn.addClass("radioSel");
        }
        //判断删除按钮的样式
        var flag = false;
        $(".detailTable .radioBtn").each(function(){
            if($(this).hasClass("radioSel")){
                flag = true;
            }
        });
        if($(".detailTable .radioBtn").size() == 1){
            flag = false;
        }
        if(flag){
            $(".delBtn").removeClass("grayBtn")
        }else{
            $(".delBtn").addClass("grayBtn");
        }
    });
    //出现选择牌号的弹出层
    $(".pinmingCol").click(function(){
        showPH.apply(this);
    });
    
}
function showPH(){
    $(".braPanel").addClass("slideInRight").one(animationEnd, function() {
            $(this).addClass("braPanelBounceIn").removeClass('slideInRight');
        });
        
        var select_data_id=$(this).parents('tr').attr('data-id');
        $('.tpfb_panel').attr('data-id',select_data_id);
        
        var brandName=$(this).find('.txt_bra').html();
        if(brandName=='牌号'){
            brandName='77';
            setBrandSearchData(brandName,'');
        }else{
            setBrandSearchData(brandName,'GP');
        }
        
        if(window.localStorage){
            localStorage.setItem("modifyTableHtml",$(".detailTable").html());
            var addBjInpStr = "";
            $(".tabNumberInp").each(function(){
                addBjInpStr += $(this).val() +  ",";
            });
            addBjInpStr = addBjInpStr.substring(0,addBjInpStr.length -1 );
            localStorage.setItem("modifyTableInpStr",addBjInpStr);
        }
        
         $(".customLink").attr("href","/mobadmin/brand/add?offerFrom=detail&offerFromId=" + offer_id+"&dataId=" + select_data_id);
}

function setBrandSearchData(brandName,brandName2){

    getBrandList(brandName,brandName2, function(data){

        var s = "";
        var s_p = "";//公告牌号内容
        var s_c = "";//自定义牌号内容
        //s_p = s_p + '<div style="background:#ccc;line-height: 1.478rem;"><span class="selectTxt" >公共牌号库</span></div>';

        $(data.public).each(function (index, el) {

            var selectInpEl_value = 'off';
            var selectRow_class = '';

            s_p = s_p + '<div class="zimuRow">' + el.lt + '</div>';

            $(el.lt_items).each(function (index2, el2) {

                /*if (index==0&&index2 == 0) {
                    selectInpEl_value = 'on';
                    selectRow_class = 'selectOn';
                }else{
                    selectInpEl_value = 'off';
                    selectRow_class = '';

                }*/
                s_p = s_p + '<div class="selectRow ' + selectRow_class + '"  value="' + el2.id + '" name="bra">' +
                    '<span style="display: none" class="txt_typ" data-id="' + el2.typeid + '">' + el2.typename + '</span>' +
                    '<div class="selectTxtWrap"><span class="selectTxt txt_pro" data-id="' + el2.brandid + '">' + el2.brand + '</span>/' +
                    '<span class="selectTxt txt_fac" data-id="' + el2.facid + '">' + el2.factory + '</span>/' +
                    '<span class="selectTxt txt_bra" data-id="' + el2.id + '">' + el2.name + '</span></div>' +
                    '<span class="radioBtn"><span class="radioCore"></span></span>' +
                    '<input type="hidden" class="selectInpEl" value="' + selectInpEl_value + '"/>' +
                    '</div>';
            });

        });

        $(data.custom).each(function (index, el) {

            var selectInpEl_value = 'off';
            var selectRow_class = '';
            if (index==0) {
                selectInpEl_value = 'on';
                selectRow_class = 'selectOn';
            }else{
                selectInpEl_value = 'off';
                selectRow_class = '';
            }
            s_c = s_c + '<div class="selectRow ' + selectRow_class + '"  value="' + el.id + '" name="bra">' +
                '<span style="display: none" class="txt_typ" data-id="' + el.typeid + '">' + el.typename + '</span>' +
                '<div class="selectTxtWrap"><span class="selectTxt txt_pro" data-id="' + el.brandid + '">' + el.brand + '</span>/' +
                '<span class="selectTxt txt_fac" data-id="' + el.facid + '">' + el.factory + '</span>/' +
                '<span class="selectTxt txt_bra" data-id="' + el.id + '">' + el.name + '</span></div>' +
                '<span class="radioBtn"><span class="radioCore"></span></span>' +
                '<input type="hidden" class="selectInpEl" value="' + selectInpEl_value + '"/>' +
                '</div>';
        });
        if(s_c!=''){

            s_c = '<div class="zimuRow">自定义牌号库</div>'+s_c;

        }

        s=s_c+s_p;
        $('.braContent').html(s);
        $('.braContent').find(".selectRow").click(function(){
            selectRowFun.apply(this);
        });
    });

}

//获取牌号数据
    function getBrandList(brandName,brandName2, callback) {
        brandName = encodeURI(brandName);
        var url = "/mobadmin/brand/searchBrand?timestamp=" + new Date().getTime() + "&brand_name=" + brandName+ "&brand_name2=" + brandName2;
        $.getJSON(url, function (data) {
            callback(data);
        });
    }


function selectRowFun(){
    if ($(this).attr("name")) {
        $(".selectRow[name=" + $(this).attr("name") + "]").removeClass("selectOn").find(".selectInpEl").val("off");
    }

    $(this).addClass("selectOn");
    $(this).find(".selectInpEl").val("on");
    selectAction.apply(this);
}

function selectAction(){    
    panelClose();
    var typ_obj = $(this).parents('.tpfb_panel').find('.selectOn').find('.txt_typ');
    var pro_obj = $(this).parents('.tpfb_panel').find('.selectOn').find('.txt_pro');
    var fac_obj = $(this).parents('.tpfb_panel').find('.selectOn').find('.txt_fac');
    var bra_obj = $(this).parents('.tpfb_panel').find('.selectOn').find('.txt_bra');

    var select_data_id=$(this).parents('.tpfb_panel').attr('data-id');

    var select_tr_obj=$('tr[data-id='+select_data_id+']');

    var select_input_obj=select_tr_obj.find('.pinmingCol');
    select_input_obj.addClass("pinmingColSel");
    var s = pro_obj.html();
    //s=s.substring(0,s.length-1);
    select_input_obj.find('.txt_pro').html(s);
    s = fac_obj.html();//s=s.substring(0,s.length-1);
    select_input_obj.find('.txt_fac').html(s);
    s = bra_obj.html();//s=s.substring(0,s.length-1);
    select_input_obj.find('.txt_bra').html(s);

    var tpfb=typ_obj.attr('data-id')+'-'+pro_obj.attr('data-id')+'-'+fac_obj.attr('data-id')+'-'+bra_obj.attr('data-id');

    select_input_obj.attr('data-tpfb',tpfb);
    select_input_obj.attr('data-type',typ_obj.html());


 //});
}
function panelClose() {
    //$(".masker").hide();
    //$(".tpfb_panel").hide();
    $(".tpfb_panel").addClass("slideOutRight").one(animationEnd, function() {
        $(this).removeClass('braPanelBounceIn slideOutRight');
    });
}

//删除按钮的功能
function delFun(){
    if($(".delBtn").hasClass("grayBtn")){
        return;
    }
    //$(".radioSel").parentsUntil("tr").remove();
    var allRadioSel = $(".radioSel");
    if($(".detailTable tr").size() - allRadioSel.size() == 1){
        allRadioSel = allRadioSel.slice(1);
    }
    allRadioSel.each(function(){
       $(this).closest("tr").remove();
    });
    $(".delBtn").addClass("grayBtn");
}

 $(".searchPop").click(function () {
    $(".searchBtn").show();
    $(".searchPop").hide();
    $(".serchInp").focus();
});

$(".serchInp").blur(function () {
    if ($.trim($(".serchInp").val()) == "") {
        $(".searchBtn").hide();
        $(".searchPop").show();
    }
});
//牌号文本框,搜索按钮
$(document).on('click',".searchBtn", function () {

    var brandName=$('.serchInp').val();

    setBrandSearchData(brandName,'');
});
$(document).on('input',".serchInp", function () {
       
    var brandName=$('.serchInp').val();
    setBrandSearchData(brandName,'');
});
//关闭报价弹窗
$(".panelClose,.cd_cancel,.masker").click(function () {
    panelClose();
});